//
//  ViewController.m
//  TouchExplorer
//
//  Created by student on 16/3/17.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
@interface ViewController ()
{
    BOOL bMove;
    CGPoint pointXY;// 距离中心点的偏移
}

@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *label3;
@property (weak, nonatomic) IBOutlet UILabel *label4;

@property (nonatomic,strong) UIView *moveView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.moveView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
    self.moveView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.moveView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 1. 触摸事件类型
    self.label1.text = @"触摸开始";
    // 2. 触摸手指数量
    self.label2.text = [NSString stringWithFormat:@"数量:%lu",(unsigned long)touches.count];
    // 3. 触摸点坐标
    UITouch *touch = touches.anyObject;// 得到触摸对象
    CGPoint p = [touch locationInView:self.view];// 得到触摸点在self.view中的坐标
    self.label3.text = [NSString stringWithFormat:@"坐标%@",NSStringFromCGPoint(p)];
    self.label4.text = [NSString
                        stringWithFormat:@"连击次数%lu",(unsigned long)touch.tapCount];
    // 1. 得到触摸点是否在moveView范围内
    bMove = CGRectContainsPoint(self.moveView.frame, p);

    if(bMove)
    {
        // 计算触摸点距离中心点的偏移
        pointXY = CGPointMake(p.x - self.moveView.center.x, p.y - self.moveView.center.y);
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    bMove = NO;
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.label1.text = @"触摸移动";
    if(bMove)
    {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.view];

        // 2. 移动它
        // 触摸点-偏移
        self.moveView.center = CGPointMake(point.x - pointXY.x, point.y - pointXY.y);
    }
}


@end
